
import numpy as np
import h5py

# pywavelet

with h5py.File("timing_samples.h5", 'r') as file_in:
    hit = file_in["hit"][()]
    nonhit = file_in["nonhit"][()]

x=hit["PulseTime"]
from ell import *

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
a, b, _= ax.hist(x, bins=1024)
c = Ell1d(a)
level = 5
rate = 0.5
d_filter = Filter.from_name('db4')

def op(d):
    s = np.sort(np.abs(d))
    L = len(s)
    t = s.tolist()[int(L * rate)]
    return d.truncate(t, soft=True)

d = c.filter(weight=d_filter, op=op, level=level)
ax.stem(a)
d.plot(axes=ax)
ax.set_title(f'reduce to {level}-level, remain {1-rate:.2%} coefs')
plt.show()
